package com.spsoft.core.utils;


import com.spsoft.core.exception.BaseException;
import lombok.experimental.UtilityClass;

import java.io.PrintWriter;
import java.io.StringWriter;

import static com.spsoft.core.constants.CommonConstants.COMMON_ERROR_MSG;


/**
 *
 */
@UtilityClass
public class ThrowableUtil {

    /**
     * 获取堆栈信息
     */
    public static String getStackTrace(Throwable throwable) {
        StringWriter sw = new StringWriter();
        try (PrintWriter pw = new PrintWriter(sw)) {
            throwable.printStackTrace(pw);
            return sw.toString();
        }
    }
    /**
     * 判断是否是自定义异常，提示不同的信息
     *
     * @param e 捕获的异常
     */
    public void throwException(Exception e) {
        if (e instanceof BaseException) {
            throw new BaseException(e.getMessage());
        } else {
            throw new BaseException(COMMON_ERROR_MSG);
        }
    }
}
